home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 52 / Amiga Format AFCD52 (Issue 136, May 2000).iso / -serious- / programming / other / pmdev / c / demos / demo.c < prev    next >
C/C++ Source or Header  |  2000-02-28  |  5KB  |  182 lines

  1. //
  2. // $VER: Demo.c 2.2 (05.09.98)
  3. //
  4. // Popup Menu example program
  5. //
  6. // ©1996-1997 Henrik Isaksson
  7. // All Rights Reserved.
  8. //
  9. // Run and click the mouse in the window and over the dragbar!
  10. // (two different menus)
  11. //
  12.  
  13. #include <intuition/intuition.h>
  14.  
  15. #include <proto/intuition.h>
  16. #include <proto/exec.h>
  17.  
  18. #include <clib/alib_protos.h>
  19.  
  20. #include <string.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23.  
  24. #include <libraries/pm.h>
  25. #include <proto/pm.h>
  26.  
  27. struct IntuitionBase    *IntuitionBase;
  28. struct GfxBase        *GfxBase;
  29. struct PopupMenuBase    *PopupMenuBase;
  30.  
  31. struct Window *w;    // This window is only needed to find out when and where the menu should appear.
  32.  
  33. ULONG __saveds __asm MenuHandlerFunc(register __a0 struct Hook *hook,
  34.                      register __a2 APTR object,
  35.                      register __a1 APTR msg)
  36. {
  37.     struct PopupMenu *pm=(struct PopupMenu *)object;
  38.     char bfr[128];
  39.  
  40.     if(pm->UserData) {
  41.         sprintf(bfr, "RUN <>NIL: %s", pm->UserData);
  42.         system(bfr);
  43.     }
  44.  
  45.     if(pm->ID==5) *((BOOL *)hook->h_Data)=FALSE;    // ID 5 means quit
  46. }
  47.  
  48. void main()
  49. {
  50.     BOOL r=TRUE;
  51.     struct IntuiMessage *im,imsg;
  52.     struct PopupMenu *p1,*p2;
  53.     struct Hook MenuHandler;
  54.  
  55.     MenuHandler.h_Entry=(HOOKFUNC)MenuHandlerFunc;
  56.     MenuHandler.h_Data=&r;
  57.  
  58.     PopupMenuBase=(struct PopupMenuBase *)OpenLibrary(POPUPMENU_NAME,POPUPMENU_VERSION);            // Open the library
  59.     if(PopupMenuBase) {
  60.         IntuitionBase=(struct IntuitionBase *)PopupMenuBase->pmb_IntuitionBase;    // We let popupmenu.library open the libraries we need
  61.         GfxBase=(struct GfxBase *)PopupMenuBase->pmb_GfxBase;            // They remain valid until the library is closed!
  62.  
  63.         p1=PMMenu("Run Demos"),
  64.             PMItem("Disable"), PMSimpleSub,
  65.                 PMInfo("This demo shows how to read the"),    End,
  66.                 PMInfo("state of a checkmark, and"),        End,
  67.                 PMInfo("how to disable & enable an item"),    End,
  68.                 PMInfo("and its submenu."),            End,
  69.                 PMBar,    End,
  70.                 PMItem("Run the demo!"),    PM_UserData,    "Disable",    End,
  71.                 End,
  72.             End,
  73.             PMItem("PullDownMenu"), PMSimpleSub,
  74.                 PMInfo("The PullDownMenu demo shows how"),    End,
  75.                 PMInfo("you can use the popupmenu.library"),    End,
  76.                 PMInfo("for pulldown menus!"),            End,
  77.                 PMBar,    End,
  78.                 PMItem("Run the demo!"),    PM_UserData,    "PullDownMenu",    End,
  79.                 End,
  80.             End,
  81.             PMItem("MenuVerify"), PMSimpleSub,
  82.                 PMInfo("This example uses the"),        End,
  83.                 PMInfo("IDCMP_MENUVERIFY flag to"),        End,
  84.                 PMInfo("combine standard intuition"),        End,
  85.                 PMInfo("menus with popupmenus int"),        End,
  86.                 PMInfo("the same window!"),            End,
  87.                 PMBar,    End,
  88.                 PMItem("Run the demo!"),    PM_UserData,    "MenuVerify",    End,
  89.                 End,
  90.             End,
  91.             PMItem("StartMenu"), PMSimpleSub,
  92.                 PMInfo("This demo shows how to put"),        End,
  93.                 PMInfo("images in you menus."),            End,
  94.                 PMInfo("You can find the menu at the"),        End,
  95.                 PMInfo("bottom of the screen."),        End,
  96.                 PMBar,    End,
  97.                 PMItem("Run the demo!"),    PM_UserData,    "StartMenu",    End,
  98.                 End,
  99.             End,
  100.             PMItem("Palette"), PMSimpleSub,
  101.                 PMInfo("Popup menus can be used to"),        End,
  102.                 PMInfo("choose colours from too..."),        End,
  103.                 PMBar, End,
  104.                 PMItem("Colours"), PMSimpleSub,
  105.                     PMCheckItem("Background", 1),    PM_ColourBox,    0,    End,
  106.                     PMCheckItem("Shadow", 1),    PM_ColourBox,    1,    End,
  107.                     PMCheckItem("Shine", 1),    PM_ColourBox,    2,    End,
  108.                     PMCheckItem("Fill", 1),    PM_ColourBox,    3,    End,
  109.                     PMCheckItem("Halfshadow", 1),    PM_ColourBox,    4,    End,
  110.                     PMCheckItem("Halfshine", 1),    PM_ColourBox,    5,    End,
  111.                     PMBar, End,
  112.                     PMItem("Use colour 1"), PM_ColourBox, 9,  End,
  113.                     PMItem("Use colour 2"), PM_ColourBox, 10, End,
  114.                     PMItem("Use colour 3"), PM_ColourBox, 11, End,
  115.                     End,
  116.                 End,
  117.                 End,
  118.             End,
  119.         End;
  120.  
  121.         p2=PMMenu("PopupMenu Demo"),
  122.             PMItem("About..."),                End,
  123.             PMTitleBar,                    End,
  124.             PMItem("Quit"),        PM_ID,        5,    End,
  125.         End;
  126.  
  127.         if(p1 && p2) {
  128.             w=OpenWindowTags(NULL,    WA_IDCMP,    IDCMP_CLOSEWINDOW|IDCMP_MOUSEBUTTONS,    // Open a little window
  129.                     WA_RMBTrap,    TRUE,
  130.                     WA_DragBar,    TRUE,
  131.                     WA_Left,    150,
  132.                     WA_Top,        100,
  133.                     WA_Width,    150,
  134.                     WA_Height,    100,
  135.                     WA_SizeGadget,    TRUE,
  136.                     WA_DepthGadget,    TRUE,
  137.                     WA_MinWidth,    150,
  138.                     WA_MaxWidth,    1280,
  139.                     WA_MinHeight,    100,
  140.                     WA_MaxHeight,    1024,
  141.                     WA_Title,    "Popup Menu Demo",
  142.                     WA_CloseGadget,    TRUE,
  143.                     TAG_DONE);
  144.             if(w) {
  145.  
  146.                 while(r) {
  147.                     WaitPort(w->UserPort);                        // Wait for a message
  148.                     while((im=(struct IntuiMessage *)GetMsg(w->UserPort))) {    // Get the message
  149.                         CopyMem(im,&imsg,sizeof(struct IntuiMessage));        // Copy the contents of it
  150.                         ReplyMsg((struct Message *)im);                // Reply the message
  151.  
  152.                         switch(imsg.Class) {
  153.                             case IDCMP_CLOSEWINDOW: r=FALSE; break;
  154.                             case IDCMP_MOUSEBUTTONS:            // The user has hit a mousebutton - time to open the menu!
  155.                                 if(imsg.Code==MENUDOWN || imsg.Code==MENUUP) {
  156.                                     if(w->MouseY>w->BorderTop) {
  157.                                         PM_OpenPopupMenu(w,
  158.                                             PM_Menu,        p1,
  159.                                             PM_Code,        imsg.Code,
  160.                                             PM_MenuHandler,        &MenuHandler,
  161.                                             TAG_DONE);
  162.                                     } else {
  163.                                         PM_OpenPopupMenu(w,
  164.                                             PM_Menu,        p2,
  165.                                             PM_Code,        imsg.Code,
  166.                                             PM_MenuHandler,        &MenuHandler,
  167.                                             TAG_DONE);
  168.                                     }
  169.                                 }
  170.                             break;
  171.                         }
  172.                     }
  173.                 }
  174.                 CloseWindow(w);
  175.             } else printf("Window error!\n");
  176.         } else printf("Menu error!\n");
  177.         if(p1) PM_FreePopupMenu(p1);
  178.         if(p2) PM_FreePopupMenu(p2);
  179.         CloseLibrary((struct Library *)PopupMenuBase);
  180.     }
  181. }
  182.